Let's start creating the project First of all we will create a project in Android Studio.After creating the project, we will use this code in Splash or MainActivity.I will use run time permissions here in SplashActivity.
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="456dp"
android:text="Splash Activity"
android:textSize="20sp"
android:textColor="#0E2ADC"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
SplashActivity.java
package com.ardeveloper.runtimepermission;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class SplashActivity extends AppCompatActivity {
private static final int PERMISSION_CODE=100;
private static String RUNTIMEPERMISSION;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU)
{
RUNTIMEPERMISSION= Manifest.permission.READ_MEDIA_IMAGES;
}
else
{
RUNTIMEPERMISSION=Manifest.permission.WRITE_EXTERNAL_STORAGE;
}
if(!isPermission())
{
Toast.makeText(this, "Please give permission ..", Toast.LENGTH_SHORT).show();
}
else if (isPermission()){
NextActivity();
}
}
private boolean isPermission() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(this,RUNTIMEPERMISSION)==PackageManager.PERMISSION_GRANTED)
{
return true;
}
else {
ActivityCompat.requestPermissions(this,new String[]{RUNTIMEPERMISSION},PERMISSION_CODE);
return false;
}
}
else{
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==PERMISSION_CODE)
{
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
if (ContextCompat.checkSelfPermission(this,
RUNTIMEPERMISSION)
== PackageManager.PERMISSION_GRANTED) {
NextActivity();
}
} else {
finish();
}
}
}
private void NextActivity() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
},2000);
}
}
Before Android 13, we had to take permission to use Write external storage and Read external storage, but after the arrival of Android 13, we did not get storage permission for their use.
Type of Media | Permission |
---|---|
Images and Photos | READ_MEDIA_IMAGES |
Videos | READ_MEDIA_VIDEO |
Audio files | READ_MEDIA_AUDIO |
In the above code we have used a variable RUNTIMEPERMISSION of a string type and after using it we have used different permissions depending on whether it is bigger or smaller than android 13.
Here we are use a condition if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU) { RUNTIMEPERMISSION= Manifest.permission.READ_MEDIA_IMAGES } else { RUNTIMEPERMISSION=Manifest.permission.WRITE_EXTERNAL_STORAGE }
By using this we can switch the permission when android 13 asks for RUNTIMEPERMISSION=Manifest.permission.WRITE_EXTERNAL_STORAGE.And if Android is greater than or equal to 13 then the permission variable will contain runtimePermissions = manifest.permissions.READ_MEDIA_IMAGES.
If the app gets the permission then it will run in the mainActivity and if the permission is denied then the app will finish because we have used the finish method. If you want to finish then you will have to ask for permission again and call this method ActivityCompat.requestPermissions(this,new String[]RUNTIMEPERMISSION},PERMISSION_CODE) again. can do
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MainActivity"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#204FDA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.ardeveloper.runtimepermission;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.app.Dialog;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji